From e40911c393ff844ac23fbeacfcae51bdfe9b5810 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Tue, 2 Feb 2021 16:50:54 -0600 Subject: [PATCH] Check the character case of table and schema names --- src/pgwui_upload_core/views/upload.py | 29 ++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/pgwui_upload_core/views/upload.py b/src/pgwui_upload_core/views/upload.py index 1eca472..dacd51d 100644 --- a/src/pgwui_upload_core/views/upload.py +++ b/src/pgwui_upload_core/views/upload.py @@ -145,7 +145,7 @@ class BaseTableUploadHandler(TabularFileUploadHandler): self.write_double_key(response) return response - def resolve_table(self, qualified_table): + def resolve_normalized_table(self, qualified_table): '''Return (schema, table) tuple of table name, or raise exception if not resolvable. ''' @@ -174,6 +174,33 @@ class BaseTableUploadHandler(TabularFileUploadHandler): raise return self.cur.fetchone() + def resolve_table(self, qualified_table): + '''Return (schema, table) tuple of table name or raise exception + if character case is wrong + ''' + (schema, table) = self.resolve_normalized_table(qualified_table) + norm_qualified_table = qualified_table.lower() + if schema: + norm_schema = schema.lower() + len_schema = len(schema) + if (norm_qualified_table[0:len_schema] == norm_schema + and qualified_table[0:len_schema] != schema): + raise upload_ex.MissingSchemaError( + 'No such schema', + (f'The schema ({qualified_table[0:len_schema]}) does ' + 'not exist, but the same-but-for-character-case ' + f'schema ({schema}) does')) + norm_table = table.lower() + len_table = len(table) + if (norm_qualified_table[- len_table:] == norm_table + and qualified_table[- len_table:] != table): + raise upload_ex.MissingTableError( + 'No such table or view', + (f'The table ({qualified_table[- len_table:]}) does ' + 'not exist, but the same-but-for-character-case ' + f'table ({table}) does')) + return (schema, table) + def good_table(self, schema, table): '''Is the supplied table or view insertable? ''' -- 2.34.1